package com.jinbiao.quasar.interceptor;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ClassUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.noear.solon.annotation.Component;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Handler;
import org.noear.solon.core.route.RouterInterceptor;
import org.noear.solon.core.route.RouterInterceptorChain;

import java.util.HashMap;

@Component
public class GlobalConverterInterceptor implements RouterInterceptor {
    @Override
    public void doIntercept(Context ctx, Handler mainHandler, RouterInterceptorChain chain) throws Throwable {
        chain.doIntercept(ctx,mainHandler);
    }

    @Override
    public Object postResult(Context ctx, Object result) throws Throwable {
        JSONConfig jsonConfig = JSONConfig.create().setDateFormat("yyyy-MM-dd HH:mm:ss");
        JSONObject jsonObject = new JSONObject(result,jsonConfig);
        return RouterInterceptor.super.postResult(ctx, JSONUtil.toBean(JSONUtil.toJsonStr(jsonObject),HashMap.class));
    }
}
